package com.zhouke.certificate.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhouke.certificate.common.Constants;
import com.zhouke.certificate.common.Result;
import com.zhouke.certificate.controller.dto.UserDTO;
import com.zhouke.certificate.entity.Certificates;
import com.zhouke.certificate.mapper.CertificatesMapper;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.zhouke.certificate.service.IUserService;
import com.zhouke.certificate.entity.User;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 周科
 * @since 2022-03-23
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private IUserService userService;
    @Resource
    private CertificatesMapper certificatesMapper;
    //登录
    @PostMapping("/login")
    public Result login(@RequestBody UserDTO userDTO) { //@RequestBody 将json转换为对象
        String username=userDTO.getUsername();
        String password=userDTO.getPassword();
        if(StrUtil.isBlank(username) || StrUtil.isBlank(password)){ //判断字符串是否为空
            return Result.error(Constants.CODE_400,"参数错误");
        }
        UserDTO dto= userService.login(userDTO);

        return Result.success(dto);
    }
    //注册
    @PostMapping("/register")
    public Result register(@RequestBody UserDTO userDTO) {
        String username=userDTO.getUsername();
        String password=userDTO.getPassword();
        if(StrUtil.isBlank(username) || StrUtil.isBlank(password)){ //判断字符串是否为空
            return Result.error(Constants.CODE_400,"参数错误");
        }
        //查看用户名是否已经被使用
        List<User> userList=userService.getUserByName(username);
        if(userList.size()>0){
            return Result.error(Constants.CODE_402,"用户名已存在");
        }
        return Result.success(userService.register(userDTO));
    }
    //新增或更新
    @PostMapping
    public Result save(@RequestBody User user) {
        // 更新用户时需要更改证书对应的值
        Certificates certificates=new Certificates();
        certificates.setName(user.getRealName());
        certificates.setSex(user.getSex());
        certificates.setIdentity(user.getIdentity());
        UpdateWrapper<Certificates> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id",user.getId());
        int i=certificatesMapper.update(certificates,updateWrapper);
        System.out.println(i);//更新行数
        return Result.success(userService.saveOrUpdate(user));
    }
    //删除
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.success(userService.removeById(id)); // table.entityPath= user
    }
    //批量删除
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids){
        return Result.success(userService.removeByIds(ids));
    }
    //查询所有
    @GetMapping
    public Result findAll() {
        return Result.success(userService.list());
    }
    //根据id查询
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(userService.getById(id));
    }
    //根据角色查询
    @GetMapping("/role/{role}")
    public Result findUsersByRole(@PathVariable String role) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role",role);
        List<User> list = userService.list(queryWrapper);
        return Result.success(list);
    }

    //个人信息
    @GetMapping("/username/{username}")
    public Result findOne(@PathVariable String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        return Result.success(userService.getOne(queryWrapper));
    }

    //分页查询
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                                 @RequestParam Integer pageSize,
                                @RequestParam(defaultValue = "") String realName,
                                @RequestParam(defaultValue = "") String college,
                                @RequestParam(defaultValue = "") String identity) {

//        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
//        if(!"".equals(realName)){
//            queryWrapper.like("real_name",realName);
//        }
//        if(!"".equals(college)){
//            queryWrapper.like("college",college);
//        }
//        if(!"".equals(identity)){
//            queryWrapper.like("identity",identity);
//        }
//        queryWrapper.orderByDesc("id");
        //获取当前登录用户新信息
//        User currentUser = TokenUtils.getCurrentUser();
//        System.out.println("=======================>"+currentUser.getNickname());

        return Result.success(userService.findPage(new Page<>(pageNum, pageSize),realName,college,identity));
    }


}

